using OfficeOpenXml;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;

namespace Super
{
	public static class EditorUtil
	{

		/*****************************************Excel相关操作*******************************************/
		public static void GenerateAllExcelStruct()
		{
			foreach (var item in ExcelNoteSO.Ins.notes)
			{
				string excelPath = item.excelPath;
				ExcelNoteNode node = ExcelNoteSO.Ins.SetTableInfo(excelPath);
				for (int i = 0; i < node.tableNames.Count; i++)
				{

					ExcelTableInfo info = ExcelUtil.GetTableInfo(excelPath, i);
					string customData = TextUtil.GetExcelClassText(info);
					AssetUtil.CreateFile(info.scriptPath, customData);
				}
			}
			AssetDatabase.Refresh();			
		}

		public static void GenerateExcelStruct(string excelPath)
		{
			if (string.IsNullOrEmpty(excelPath))
			{
				Debug.Log("Excel路径不能为空");
				return;
			}

			ExcelNoteNode node = ExcelNoteSO.Ins.SetTableInfo(excelPath);
			for (int i = 0; i < node.tableNames.Count; i++)
			{

				ExcelTableInfo info = ExcelUtil.GetTableInfo(excelPath, i);
				if (info.keys.Contains("enumName"))
				{
					Debug.Log(excelPath + "表格包含enumName主键，请选择其他表格");
					return;
				}
				string customData = TextUtil.GetExcelClassText(info);
				AssetUtil.CreateFile(info.scriptPath, customData);
			}
			AssetDatabase.Refresh();
			Debug.Log("成功生成" + excelPath + "表格的数据类");
		}

		public static void NewStructAndLoadExcel(string excelPath)
		{
			ExcelNoteNode node = ExcelNoteSO.Ins.SetTableInfo(excelPath);
			for (int i = 0; i < node.tableNames.Count; i++)
			{

				ExcelTableInfo info = ExcelUtil.GetTableInfo(excelPath, i);
				if (info.keys.Contains("enumName"))
				{
					Debug.Log(excelPath + "表格包含enumName主键，请选择其他表格");
					return;
				}
				string customData = TextUtil.GetExcelClassText(info);
				AssetUtil.CreateFileUnRefresh(info.scriptPath, customData);
			}

			ReloadScriptManager.Ins.AddMethod("LoadExcelData", excelPath);
			AssetDatabase.Refresh();
		}

		public static void GenerateExcelEnum(string excelPath)
		{
			if (string.IsNullOrEmpty(excelPath))
			{
				Debug.Log("Excel路径不能为空");
				return;
			}

			List<string> tableNames = ExcelUtil.GetExcelTableNames(excelPath);
			for (int i = 0; i < tableNames.Count; i++)
			{
				ExcelTableInfo info = ExcelUtil.GetTableInfo(excelPath, i);
				if (!info.keys.Contains("enumName"))
				{
					Debug.Log(excelPath + "表格无enumName主键，无法生成枚举文件，请选择其他表格");
					return;
				}
				string customData = TextUtil.GetExcelEnumText(info);
				string path = string.Format(PathSettingSO.Ins.excelEnumPath, tableNames[i]);
				AssetUtil.CreateFile(path, customData);
			}
			AssetDatabase.Refresh();
			Debug.Log("成功生成" + excelPath + "表格的枚举");
		}

		public static void RemoveExcelSO(string excelPath)
		{
			ExcelNoteSO.Ins.Remove(excelPath);
			Debug.Log("移除" + excelPath + "的Asset文件");
		}

		public static void RemoveAllExcelSO()
		{
			foreach (var item in ExcelNoteSO.Ins.notes)
			{
				RemoveExcelSO(item.excelPath);
			}
			ExcelNoteSO.Ins.notes.Clear();
			Debug.Log("移除所有Excel的Asset文件");
		}

		public static void WriteAllExcel()
		{
			foreach (var item in ExcelNoteSO.Ins.notes)
			{
				ExcelUtil.WriteExcel(item.excelPath);
			}
			Debug.Log("所有Asset文件覆写Excel!!!");
		}

	}
}
